package com.winit.datasync.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.winit.datasync.dao.UpsConfigDao;
import com.winit.datasync.entity.UpsConfig;
import com.winit.datasync.utils.ConstantUtils;
import com.winit.datasync.utils.DateUtil;

@Repository("upsConfigDao")
public class UpsConfigDaoImpl implements UpsConfigDao {

	private static Logger LOGGER = LoggerFactory.getLogger(UpsConfigDaoImpl.class);
	@Autowired
	private CassandraTemplate cassandraTemplate;

	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private MongoTemplate mongoTemplate;

	@Override
	public void batchInsert(final List<UpsConfig> list, int dataSourceType) {
		if (list == null || list.size() == 0)
			return;
		if (3 == dataSourceType) {
			return;
		} else if (1 == dataSourceType) {
			jdbcTemplate.batchUpdate(ConstantUtils.ORACLE_BATCH_INSERT,
					new BatchPreparedStatementSetter() {

						@Override
						public void setValues(PreparedStatement ps, int index) throws SQLException {
							UpsConfig upsConfig = list.get(index);
							ps.setString(1, upsConfig.getId());
							ps.setString(2, upsConfig.getName());
							ps.setString(3, upsConfig.getDescription());
							ps.setLong(4, upsConfig.getOrg_id());
							ps.setDate(5,
									DateUtil.convertUtilDateToSqlDate(upsConfig.getCreated()));
							ps.setString(6, upsConfig.getCreatedBy());
						}

						@Override
						public int getBatchSize() {
							return list.size();
						}
					});
		} else {
			mongoTemplate.insert(list, UpsConfig.class);
		}
		
		LOGGER.info("batchInsert->插入成功  11");
	}

	@Override
	public void delete(UpsConfig upsConfig, int dataSourceType) {
		if (3 == dataSourceType) {
			return;
		} else if (1 == dataSourceType) {
			jdbcTemplate.execute(ConstantUtils.ORACLE_DELETE);
		} else {
			Query query = new Query();
			query.addCriteria(Criteria.where("_id").is(upsConfig.getId()));

			mongoTemplate.remove(query, UpsConfig.class);
			
		}
	}

	@Override
	public List<UpsConfig> getByParams(HashMap<String, String> params, int dataSourceType) {

		StringBuilder sBuilder = new StringBuilder(ConstantUtils.ORACLE_GET_ALL);
		if (params != null && !params.isEmpty()) {
			sBuilder.append(ConstantUtils.WHERE);

			// 拼接
			for (String key : params.keySet()) {
				sBuilder.append(StringUtils.EMPTY).append(key).append(ConstantUtils.EQUAL)
						.append(params.get(key));
			}

		}

		if (3 == dataSourceType) {
			return cassandraTemplate.select(sBuilder.toString(), UpsConfig.class);
		} else if (1 == dataSourceType) {
			return null;
		} else {
			return null;
		}

	}

	@Override
	public List<UpsConfig> getAll(int dataSourceType) {
		if (3 == dataSourceType) {
			return cassandraTemplate.select(ConstantUtils.CASSANDRA_GET_ALL, UpsConfig.class);
		} else if (1 == dataSourceType) {
			return jdbcTemplate.query(ConstantUtils.ORACLE_GET_ALL, new RowMapper<UpsConfig>() {

				@Override
				public UpsConfig mapRow(ResultSet rs, int rowNum) throws SQLException {

					UpsConfig upsConfig = new UpsConfig();
					upsConfig.setId(rs.getString("id"));
					upsConfig.setName(rs.getString("name"));
					upsConfig.setDescription(rs.getString("description"));
					upsConfig.setOrg_id(rs.getLong("org_id"));
					upsConfig.setCreated(DateUtil.convertSqlDateToUtilDate(rs.getDate("created")));
					upsConfig.setCreatedBy(rs.getString("createdby"));

					return upsConfig;
				}
			});
		} else {
			return mongoTemplate.findAll(UpsConfig.class);
		}

	}

}
